﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using System.Data.SqlClient;

namespace DiChanAPI.Controllers
{
    public class UserController : ApiController
    {
        // GET api/user
        [HttpGet]
        [ActionName("GetUsers")]
        public IEnumerable<Users> GetUsers(string name)
        {
            
            if (name == null || name == "")
            {
                return new UserBLL().Getlist();
            }
            else
            {
                return new UserBLL().Getlist().Where(n => n.Users_Name.Contains(name));
            }
        }

        // GET api/user/5
        [HttpGet]
        [ActionName("GetUserByID")]
        public IEnumerable<Users> GetUserByID(int id) 
        {
            return new UserBLL().Getlist().Where(n => n.Users_Id == id);
        }

        // POST api/user
        [HttpPost]
        [ActionName("PostUsers")]
        public bool PostUsers([FromBody]Users users)
        {
            SqlParameter[] parame ={
                                      new SqlParameter("@Users_Name",users.Users_Name),
                                      new SqlParameter("@Users_Sex",users.Users_Sex),
                                      new SqlParameter("@Users_IDCard",users.Users_IDCard),
                                      new SqlParameter("@Users_TEL",users.Users_TEL),
                                      new SqlParameter("@Users_Email",users.Users_Email),
                                       new SqlParameter("@Users_Account",users.Users_Account),
                                        new SqlParameter("@Roles_id",users.Roles_id),
                                         new SqlParameter("@Department_Id",users.Department_Id),
                                         new SqlParameter("@Users_Pwd","123"),
                                  };
            return new RolesBLL().AddRole(parame);
        }

        // PUT api/user/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/user/5
        [HttpDelete]
        [ActionName("DeleteUser")]
        public bool DeleteUser(int id)
        {
            Users user = new Users ();
            user.Users_Id = id;
            return new UserBLL().DeleteByID(user); 
        }
    }
}
